package com.yupi.kqapibackend.controller;

import com.yupi.kqapibackend.common.BaseResponse;
import com.yupi.kqapibackend.common.ResultUtils;
import com.yupi.kqapibackend.model.entity.User;
import com.yupi.kqapibackend.model.vo.TokenVO;
import com.yupi.kqapibackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    UserService userService;

    @GetMapping("/get")
    public BaseResponse<TokenVO> getToken(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        User byId = userService.getById(loginUser.getId());
        String accessKey = byId.getAccessKey();
        TokenVO tokenVO = new TokenVO();
        tokenVO.setUserId(byId.getId());
        tokenVO.setToken(accessKey);
        return ResultUtils.success(tokenVO);
    }
}
